﻿using BCCommon;
using BCCommon.Enums;
using BCData.Trading.Purchase.GoodsPurchaseContractAttachment;
using BCDto.Trading.Purchase.GoodsPurchaseContractAttachment;
using BCEntity.Trading.Purchase.GoodsPurchaseContractAttachment;
using BCEntity.Common.EntityNameConst;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BCData.Trading.Purchase.GoodsPurchase;
using BCDto.Sim.Admin.Admin;

namespace BCService.Trading.Purchase.GoodsPurchaseContractAttachment
{
    public class GoodsPurchaseContractAttachmentService : IGoodsPurchaseContractAttachmentService
    {
        private readonly IDatabaseContext databaseContext;
        private readonly IRedisService redisService;
        private readonly IGoodsPurchaseContractAttachmentData goodsPurchaseContractAttachmentData;
        private readonly IGoodsPurchaseData goodsPurchaseData;
        private readonly string key = string.Format("{0}", EntityNameConst.GoodsPurchaseContractAttachmentEntity);

        public GoodsPurchaseContractAttachmentService(IDatabaseContext databaseContext,
            IRedisService redisService,
            IGoodsPurchaseContractAttachmentData goodsPurchaseContractAttachmentData, 
            IGoodsPurchaseData goodsPurchaseData)
        {
            this.databaseContext = databaseContext;
            this.redisService = redisService;
            this.goodsPurchaseContractAttachmentData = goodsPurchaseContractAttachmentData;
            this.goodsPurchaseData = goodsPurchaseData;
        }

        #region 检查商品采购合同附件是否存在
        public bool Exists(long goodsPurchaseContractAttachmentId)
        {
            return this.goodsPurchaseContractAttachmentData.Exists(goodsPurchaseContractAttachmentId).GetAwaiter().GetResult();
        }
        #endregion

        #region 添加商品采购合同附件
        public GoodsPurchaseContractAttachmentDto Add(GoodsPurchaseContractAttachmentRequestDto requestDto,AdminDto adminDto)
        {
            if (!this.goodsPurchaseData.Exists(requestDto.GoodsPurchaseId).Result)
            {
                throw new ArgumentException("此商品采购单不存在!");
            }
            var dto = this.goodsPurchaseData.GetEntity(requestDto.GoodsPurchaseId).Result;

            var entity = requestDto.As<GoodsPurchaseContractAttachmentEntity>();
            entity.GoodsPurchaseNo = dto.GoodsPurchaseNo;
            entity.PurchaseContractNo = dto.PurchaseContractNo;
            entity.CreateAdminName = adminDto.FullName;
            entity.CreateAdminId = adminDto.AdminId;
            var result = this.goodsPurchaseContractAttachmentData.Add(entity).Result;

            return result.As<GoodsPurchaseContractAttachmentDto>();
        }
        #endregion
       
        #region 删除商品采购合同附件
        public bool Delete(long goodsPurchaseContractAttachmentId)
        {
            if (!this.Exists(goodsPurchaseContractAttachmentId))
            {
                throw new ArgumentException("此记录不存在!");
            }

            var result = this.goodsPurchaseContractAttachmentData.Delete(goodsPurchaseContractAttachmentId).GetAwaiter().GetResult();
            string cacheKey = this.redisService.GetKey(key, goodsPurchaseContractAttachmentId);
            this.redisService.DeleteAsync(cacheKey);
            return result;
        }
        #endregion

        #region 获取商品采购合同附件
        public GoodsPurchaseContractAttachmentDto Get(long goodsPurchaseContractAttachmentId)
        {
            string cacheKey = this.redisService.GetKey(key, goodsPurchaseContractAttachmentId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.goodsPurchaseContractAttachmentData.GetEntity(goodsPurchaseContractAttachmentId).GetAwaiter().GetResult();
                return data;
            }, TimeSpan.FromMinutes(30));
            return result.As<GoodsPurchaseContractAttachmentDto>();
        }
        #endregion

        #region 获取商品采购合同附件列表
        public IEnumerable<GoodsPurchaseContractAttachmentDto> GetList(long goodsPurchaseId)
        {
            string cacheKey = this.redisService.GetKey(key, goodsPurchaseId);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                return this.goodsPurchaseContractAttachmentData.GetList(goodsPurchaseId).GetAwaiter().GetResult();
            }, TimeSpan.FromMinutes(30));
            return result.As<IEnumerable<GoodsPurchaseContractAttachmentDto>>();
        }
        #endregion

        #region 获取商品采购合同附件分页
        public PageableList<GoodsPurchaseContractAttachmentDto> Query(long? goodsPurchaseId, string purchaseContractNo, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            string cacheKey = this.redisService.GetKey(key, goodsPurchaseId, purchaseContractNo, start, end, sortName, ascending, pageIndex, pageSize);
            var result = this.redisService.TryGet(cacheKey, () =>
            {
                var data = this.goodsPurchaseContractAttachmentData.Query(goodsPurchaseId, purchaseContractNo, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();
                return new PageableList<GoodsPurchaseContractAttachmentDto>
                {
                    Count = data.Item2,
                    Items = data.Item1.As<IEnumerable<GoodsPurchaseContractAttachmentDto>>(),
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    ServerTime = DateTime.Now
                };
            }, TimeSpan.FromMinutes(30));

            return result;
        }
        #endregion
    }
}